package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.Menu;
import com.ytjj.qmyx.admin.model.request.ButtonRequest;
import com.ytjj.qmyx.admin.model.response.MenuTreeResponse;
import com.ytjj.qmyx.admin.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @RequiresPermissions("menu:create")
    @PostMapping(value = "/create")
    @OperLog(operModule = "菜单模块", operDesc = "添加菜单", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult create(@RequestBody Menu menu) {
        int count = menuService.create(menu);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @RequiresPermissions("menu:update")
    @PostMapping(value = "/update")
    @OperLog(operModule = "菜单模块", operDesc = "修改菜单", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult update(@RequestBody Menu menu) {
        int count = menuService.update(menu);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据ID获取菜单信息
     * @param id
     * @return
     */
    @GetMapping(value = "getMenu")
    public CommonResult<Menu> getMenuById(@RequestParam Integer id) {
        Menu menu = menuService.getMenuById(id);
        return CommonResult.success(menu);
    }

    /**
     * 根据 id 删除菜单信息
     * @param id
     * @return
     */
    @RequiresPermissions("menu:delete")
    @PostMapping(value = "/delete")
    @OperLog(operModule = "菜单模块", operDesc = "根据id删除菜单信息", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult delete(@RequestParam Integer id) {
        int count = menuService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 返回所有树状结构的菜单列表
     * @return
     */
    @RequiresPermissions("menu:treeList")
    @GetMapping(value = "/treeList")
    public CommonResult<List<MenuTreeResponse>> treeList() {
        List<MenuTreeResponse> list = menuService.treeList();
        return CommonResult.success(list);
    }

    /**
     * 修改菜单权限（是否显示）
     * @param id
     * @return
     */
    @GetMapping(value = "/updatePerm")
    @OperLog(operModule = "菜单模块", operDesc = "修改菜单权限(是否显示)", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updatePerm(@RequestParam Integer id) {
        int count = menuService.updatePerm(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 拖拽菜单到指定位置
     * @param checkedId
     * @param pId
     * @return
     */
    @GetMapping(value = "/updateTreeList")
    @OperLog(operModule = "菜单模块", operDesc = "拖拽菜单到指定位置", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateTreeList(@RequestParam Integer checkedId, @RequestParam Integer pId) {
        int count = menuService.updateTreeList(checkedId,pId);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    // -------------------------------------------------------------------菜单按钮权限分配------------------------------------------------------------------------------

    /**
     * 查询所有的按钮
     *
     * @return
     */
    @PostMapping(value = "/buttonList")
    public CommonResult<List<Menu>> buttonList(@RequestBody ButtonRequest buttonRequest) {
        List<Menu> menus = menuService.buttonList(buttonRequest);
        return CommonResult.success(menus);
    }


    @GetMapping("/editButton")
    public CommonResult editButton(@RequestParam("id") Integer id,
                                   @RequestParam("apiCode") String apiCode) {
        menuService.editButton(id, apiCode);
        return CommonResult.success();
    }

    /**
     * 查询所有子菜单
     *
     * @return
     */
    @RequiresPermissions("menu:queryChildrenList")
    @GetMapping(value = "/queryChildrenList")
    public CommonResult<List<Menu>> queryChildrenList() {
        List<Menu> menus = menuService.queryChildrenList();
        return CommonResult.success(menus);
    }

    /**
     * 创建按钮
     * @return
     */
    @RequiresPermissions("menu:createButton")
    @PostMapping(value = "/createButton")
    public CommonResult createButton(@RequestBody Menu menu) {
        int count = menuService.createButton(menu);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 编辑按钮
     * @return
     */
    @PostMapping(value = "/updateButton")
    public CommonResult updateButton(@RequestBody Menu menu) {
        int count = menuService.updateButton(menu);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除按钮
     * @return
     */
    @GetMapping(value = "/deleteButton")
    public CommonResult deleteButton(@RequestParam Integer id) {
        int count = menuService.deleteButton(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 停用/启用 按钮
     * @return
     */
    @GetMapping(value = "/updateButtonStatus")
    public CommonResult updateButtonStatus(@RequestParam Integer id) {
        int count = menuService.updateButtonStatus(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
}
